package com.wlz.service.impl;

import com.wlz.model.BaseResponse;
import com.wlz.service.IVoteAccessService;
import com.wlz.service.TemplateMsgService;
import com.wlz.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  邀请好友投票成功   服务实现类
 * </p>
 *
 * @author wlz
 * @since 2020-03-15
 */
@Service
public class VoteAccessServiceImpl implements IVoteAccessService {

    private static Logger logger = LoggerFactory.getLogger(VoteAccessServiceImpl.class);

    @Autowired
    TemplateMsgService templateMsgService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int runReport(String openId,String userName,String phone,Integer applyId) {

        this.voteAccess(openId,userName,phone,applyId);
//        this.voteAccess("o3wogwz-1emerlxLAJ89wi0IMnqc","00001",14);
        return 1;
    }

    public void voteAccess(String openId,String userName,String phone,Integer applyId) {
        logger.info("voteAccess start.......");
        String templateId = "7crB0X4VlaQDk_JuVvAG7FjFihiurfZooAQq7c8BC5U";
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> first = new HashMap<String, Object>();
        first.put("value","你好，有人通过您的邀请，已投票成功。");
        dataMap.put("first",first);
        Map<String,Object> keyword1 = new HashMap<String, Object>();
        keyword1.put("value",userName);
        dataMap.put("keyword1",keyword1);
        Map<String,Object> keyword2 = new HashMap<String, Object>();
        keyword2.put("value",phone);
        dataMap.put("keyword2",keyword2);
        Map<String,Object> keyword3 = new HashMap<String, Object>();
        keyword3.put("value", DateUtil.format(new Date(),DateUtil.DATE_PATTERN) + "   投票");
        dataMap.put("keyword3", keyword3);
        Map<String,Object> remark = new HashMap<String, Object>();
        remark.put("value","记得邀请后可为自己再投一票哦，参与逆行先锋评选的你是最棒的！");
        dataMap.put("remark",remark);
        Map<String,Object> miniprogram = new HashMap<String, Object>();
        miniprogram.put("appid","wxe713317872a2d636");
        miniprogram.put("pagepath","/pages/vote/detail/detail?id=" + applyId);
        this.sendGZHTemplateMsg(openId,templateId,null,miniprogram,dataMap);
        logger.info("voteAccess end.......");
    }

    /**
     *推送（公众号） 模板消息
     *
     * @param openId 用户openId
     * @param templateId 所需下发的模板消息的id
     * @param pageUrl  点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
     * @param miniprogram  跳小程序所需数据，不需跳小程序可不用传该数据
     *          appid       所需跳转到的小程序appid（该小程序appid必须与发模板消息的公众号是绑定关联关系，暂不支持小游戏）
     *          pagepath    所需跳转到小程序的具体页面路径，支持带参数,（示例index?foo=bar），要求该小程序已发布，暂不支持小游戏
     * data   模板内容，不填则下发空模板。具体格式请参考示例。
     * @return
     */
    public int sendGZHTemplateMsg(String openId, String templateId, String pageUrl, Map<String,Object> miniprogram, Map<String,Object> dataMap) {

        BaseResponse response  = templateMsgService.sendGZHTemplateMsg(openId,templateId,pageUrl,miniprogram,dataMap);
        if (response != null) {
            if (response.getStatusCode() == 0) {
                logger.info("sendGZHTemplateMsg msg>>>"+response.getMsg());
            }else {
                logger.info("sendGZHTemplateMsg msg>>>"+response.getMsg());
            }
        }
        return response.getStatusCode();
    }

}
